#include "MessageEngine.h"
#include <iostream>
#include <log.h>
#include "EmulatorImpl.h"
#include "utils.h"

namespace kq{



	MessageEngine::MessageEngine(MessageEngineConfig messageEngineConfig, int authCode, int64_t qqId, const char* qqName)
		:authCode(authCode), qqId(qqId), qqName(qqName), messageEngineConfig(messageEngineConfig)
	{
		
	}

	MessageEngine::~MessageEngine(void)
	{


	}

	void MessageEngine::start()
	{
		LOGI("MessageEngine start");
		std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(messageEngineConfig.controllerAddr, grpc::InsecureCredentials());
		controllerClient_ = qq::message::Controller::NewStub(channel);
		LOGI("MessageEngine controllerClient_ start [OK]");
		emulatorServerThread_.reset(new std::thread(std::bind(&MessageEngine::emulatorServerLoop, this)));
		LOGI("MessageEngine start [OK]");
	}

	void MessageEngine::stop()
	{
		LOGI("MessageEngine stop");
		emulatorServer_->Shutdown();
		emulatorServerThread_->join();
		LOGI("MessageEngine stop [OK]");

	}

	void MessageEngine::emulatorServerLoop()
	{
		LOGI("MessageEngine emulatorServer start");
		EmulatorImpl emulatorImpl(*this);
		grpc::ServerBuilder builder;
		builder.AddListeningPort(messageEngineConfig.emulatorAddr, grpc::InsecureServerCredentials());
		builder.RegisterService(&emulatorImpl);
		std::unique_ptr<grpc::Server> emulatorServer_(builder.BuildAndStart());
		LOGI("MessageEngine emulatorServer start [OK]");
		emulatorServer_->Wait();
		LOGI("MessageEngine emulatorServer stop [OK]");
	}

	bool MessageEngine::eventPrivateMsg(int32_t subType, int32_t sendTime, int64_t fromQQ, const char *msg, int32_t font){
		LOGD(fromQQ << " eventPrivateMsg " << msg);
		GBK2UTF8(utf8_output, msg);
		qq::message::ControllerPrivateMsg controllerPrivateMsg;
		controllerPrivateMsg.set_fromqq(fromQQ);
		controllerPrivateMsg.set_msg(utf8_output);
		controllerPrivateMsg.set_sendtime(sendTime);
		controllerPrivateMsg.set_subtype(subType);
		grpc::ClientContext context;
		qq::message::Gvoid response;
		grpc::Status status = controllerClient_->privateMsg(&context, controllerPrivateMsg, &response);
		LOGD(fromQQ << " eventPrivateMsg trans " << status.ok());
		return status.ok();
	}
	bool MessageEngine::eventGroupMsg(int32_t subType, int32_t sendTime, int64_t fromGroup, int64_t fromQQ, const char *fromAnonymous, const char *msg, int32_t font){
		LOGD(fromQQ << " eventGroupMsg " << msg);
		GBK2UTF8(utf8_output, msg);
		qq::message::ControllerGroupMsg controllerGroupMsg;
		controllerGroupMsg.set_fromqq(fromQQ);
		controllerGroupMsg.set_msg(utf8_output);
		controllerGroupMsg.set_sendtime(sendTime);
		controllerGroupMsg.set_fromgroup(fromGroup);
		grpc::ClientContext context;
		qq::message::Gvoid response;
		grpc::Status status = controllerClient_->groupMsg(&context, controllerGroupMsg, &response);
		LOGD(fromQQ << " eventGroupMsg trans " << status.ok());
		return status.ok();
	}
	bool MessageEngine::eventDiscussMsg(int32_t subType, int32_t sendTime, int64_t fromDiscuss, int64_t fromQQ, const char *msg, int32_t font){
		LOGD(fromQQ << " eventDiscussMsg " << msg);
		GBK2UTF8(utf8_output, msg);
		qq::message::ControllerDiscussMsg controllerDiscussMsg;
		controllerDiscussMsg.set_fromqq(fromQQ);
		controllerDiscussMsg.set_msg(utf8_output);
		controllerDiscussMsg.set_sendtime(sendTime);
		controllerDiscussMsg.set_fromdiscuss(fromDiscuss);
		grpc::ClientContext context;
		qq::message::Gvoid response;
		grpc::Status status = controllerClient_->discussMsg(&context, controllerDiscussMsg, &response);
		LOGD(fromQQ << " eventDiscussMsg trans " << status.ok());
		return status.ok();
	}


}